import threading
import time

with open('runtime.txt', mode='a+', encoding='utf-8') as file:
    # 将文件偏移量位置放在文件头
    file.seek(0)
    n = 1
    for row in file:
        n += 1
    while True:
        current_time = time.strftime('%Y-%m-%d %H:%M:%S \n', time.localtime())
        print(current_time, end='')
        file.writelines("%d:%s" % (n, current_time))
        # 将时间写入磁盘去
        file.flush()
        n += 1
        # 当前线程睡眠1s
        time.sleep(1)
